<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>设置角色</title>
    <script type="text/javascript" src="/js/jquery.js"></script>
</head>
<body>

<div>
    <div >
        角色名称pid:${pid}：
        <input type="hidden" id="J_id" name ="id" value ="{id}" />
        <input type="text" id = "J_roleName" name = "name" />
        <br>
        权限列表
        <ul id ="J_permission">

        </ul>
    </div>

</div>

<script>
    $(function(){
        var id = "${id}";
        if(id == 0){
            getPermissionList();
        }else{
            getRoleDetail(id)
        }

    });

    function getRoleDetail(id){
        var data = {id:id};
        var url = "/role/role_detail";
        $.ajax({
            type: "POST",
            dataType: 'json',
            async: false,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            url: url,
            success: function(res){
                if(res.code = "200"){
                   //操作
                  var detail = res.data.detail;
                  var list = res.data.per_list;
                  console.log(list);
                  $("#J_roleName").val(detail.name);
                  for(var i =0;i<list.length;i++){
                      var shtml = "";
                      if(list[i]['selected'] == 1){
                          shtml = '<li><div>'+list[i]['name']+'</div><input type="checkbox"  checked value = "'+list[id]['id']+'" /></li>\n';
                      }else{
                          shtml = '<li><div>'+list[i]['name']+'</div><input type="checkbox"  value = "'+list[id]['id']+'" /></li>\n';
                      }

                    $("#J_permission").append(shtml);
                  }
                }else{
                    alert(res.msg);
                }
            },
            error: function(e){

            },
            complete: function(e){
            }
        });
    }

    function getPermissionList(){
        var url="/permission/list";
        $.ajax({
            type: "POST",
            dataType: 'json',
            async: false,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            url: url,
            success: function(res){
                console.log(res);
                if(res.code = "200"){
                    //操作
                    var list =res.data.list;
                    for(var i =0;i<list.length;i++){
                        var shtml = "";
                        if(list[i]['selected'] == 1){
                            shtml = '<li><div>'+list[i]['name']+'</div><input type="checkbox"  checked value = "'+list[id]['id']+'" /></li>\n';
                        }else{
                            shtml = '<li><div>'+list[i]['name']+'</div><input type="checkbox"  value = "'+list[id]['id']+'" /></li>\n';
                        }
                        $("#J_permission").append(shtml);
                    }
                }else{
                    alert(res.msg);
                }
            },
            error: function(e){

            },
            complete: function(e){
            }
        });
    }

</script>
</body>
</html>
